package com.liuwa.knowledge.service.impl;

import com.liuwa.common.constant.SysConstants;
import com.liuwa.common.core.service.impl.CurdServiceImpl;
import com.liuwa.common.exception.ExistException;
import com.liuwa.common.exception.NotExistException;
import com.liuwa.common.exception.ServiceException;
import com.liuwa.common.utils.StringUtils;
import com.liuwa.common.utils.sign.Md5Utils;
import com.liuwa.common.utils.spring.SpringUtils;
import com.liuwa.knowledge.dao.UserDao;
import com.liuwa.knowledge.domain.User;
import com.liuwa.knowledge.service.UserService;
import com.liuwa.sys.domain.Oauth;
import com.liuwa.sys.service.OauthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户Service业务层处理
 * 
 * @author liuwa
 * @date 2023-12-12 16:00:20
 */
@Service("userService")
public class UserServiceImpl extends CurdServiceImpl<Long, UserDao, User> implements UserService{


    @Resource
    private UserDao userDao;

    @Autowired
    private OauthService oauthService;






    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public User findByPhone(String phone) {
        return userDao.findByPhone(phone);
    }

    @Override
    public void updatePassword(User user) {
        userDao.updatePassword(user);
    }

    @Override
    public void changePhone(Long userId, String phone) {
        User exist = findByPhone(phone);
        if(exist != null){
            throw new ExistException("该手机号已存在");
        }
        User user = get(userId);
        if(user == null){
            throw new NotExistException("当前用户不存在或已删除");
        }
        if(user.getPhone().equals(phone)){
            throw new ServiceException("新旧手机号不能是同一个号码");
        }

        User updater = new User();
        updater.setUsername(phone);
        updater.setPhone(phone);
        updater.setUserId(userId);
        updateSelective(updater);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void block(Long userId) {
        User updater = new User();
        updater.setUserId(userId);
        updater.setStatus(SysConstants.DISABLE);
        updateSelective(updater);

        //tokenService.kick();

    }

    @Override
    public void unblock(Long userId) {
        User updater = new User();
        updater.setUserId(userId);
        updater.setStatus(SysConstants.ENABLE);
        updateSelective(updater);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User authRegister(String openId, String platform) {
        String name = "小善" + (System.currentTimeMillis() / 1000 - 1744814071);

        User user = new User();
        user.setUsername(openId);
        user.setNickName(name);
        user.setStatus(SysConstants.ENABLE);
        user.setPhone("");
        user.setAvatar("");
        user.setSalt(StringUtils.getRandomString(4));
        user.setPassword(Md5Utils.hash(openId + user.getSalt()));
        SpringUtils.getAopProxy(this).insert(user);


        Oauth oauth = new Oauth();
        oauth.setPlatform(platform);
        oauth.setOpenId(openId);
        oauth.setUserId(user.getUserId());
        oauthService.insert(oauth);
        return user;
    }
}
